package com.liyunc.demo.bundle.automation;

import com.liyunc.demo.bundle.automation.service.TransactionHookService;
import com.liyunc.demo.comp.common.db.TransactionHook;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManager;

/**
 * @author liyuncong
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-03-04
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-03-04                       Created
 */
@Configuration
@ConditionalOnProperty(name = "liyunc.db.hibernate.auditable.autofill.enabled",
    havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(value = {AuditableAutofillConfigProperties.class})
public class AuditableAutofillConfiguration {
    private static AuditableAutofillConfigProperties autofillConfigProperties;
    private static EntityManager entityManager;
    private static TransactionHookService transactionHookService;

    public static AuditableAutofillConfigProperties getAutofillConfigProperties() {
        return autofillConfigProperties;
    }

    public static EntityManager getEntityManager() {
        return entityManager;
    }

    public AuditableAutofillConfiguration(
        AuditableAutofillConfigProperties configProperties,
        EntityManager entityManager
    ) {
        AuditableAutofillConfiguration.autofillConfigProperties = configProperties;
        AuditableAutofillConfiguration.entityManager = entityManager;
    }

    public static TransactionHookService getTransactionHookService() {
        return transactionHookService;
    }

    @Bean
    public TransactionHook transactionHook() {
        transactionHookService = new TransactionHookService();
        return transactionHookService;
    }
}
